package com.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group.entity.ProjectInside;
import com.group.entity.ProjectOutside;
import com.group.mapper.GroupsInformationMapper;
import com.group.mapper.ProjectInsideMapper;
import com.group.mapper.ProjectOutsideMapper;
import com.group.service.GroupsInformationService;
import com.group.service.ProjectInsideService;
import com.group.service.ProjectUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
@RequestMapping("/project-user")
public class ProjectUserController {

    @Autowired
    GroupsInformationMapper groupsInformationMapper;

    @Autowired
    GroupsInformationService groupsInformationService;

    @Autowired
    ProjectInsideService projectInsideService;
    @Autowired
    ProjectOutsideMapper projectOutsideMapper;

    @Autowired
    ProjectUserService projectUserService;

    /**
     * 查询用户的项目，包括参与的以及自己创建的
     *
     * @param session 通过session来获取当前用户登录的Id
     * @param model   返回前端视图层
     * @return
     */
    @RequestMapping("/myProject")
    public String myJoinProject(HttpSession session, Model model) {
        List<ProjectOutside> projectOutsideList = new ArrayList<>();
        List<ProjectInside> projectInsideList = new ArrayList<>();
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("master_id", currentUserId);
        //查询队长Id为自己的项目
        List isMasterList = projectOutsideMapper.selectList(wrapper);
        model.addAttribute("isMasterList", isMasterList);

        //查询队员为自己的项目Id
        List<Integer> memberTogroupId = groupsInformationService.findMember(currentUserId);
        for (Integer groupId : memberTogroupId) {
            QueryWrapper wrapper1 = new QueryWrapper();
            ProjectOutside projectOutside = new ProjectOutside();
            wrapper1.eq("groupId", groupId);
            ProjectInside projectInside = projectInsideService.getOne(wrapper1);
            projectInsideList.add(projectInside);
        }
        model.addAttribute("isMemberList", projectInsideList);
        return "My_Xiangmu";
    }

    @RequestMapping("/modifypwd")
    public String redirect() {
        return "";
    }

    @RequestMapping("/seeMyProject/{projectId}")
    @ResponseBody
    public Integer seeMyProject(@PathVariable("projectId") Integer projectId) {
        return projectId;
    }

}

